package org.de.maeyer.sampler;

import org.apache.commons.lang.mutable.MutableLong;

/**
 * @author Peter De Maeyer
 */
public class AutoCategorizingObjectHistogramAccumulator extends ObjectHistogramAccumulator {
	
	private final int categoryBound;

	/**
	 * @param categoryBound
	 *            the max # categories this distribution may create, or <code>-1</code> if no limit.
	 * @param categoryField
	 */
	public AutoCategorizingObjectHistogramAccumulator(int categoryBound) {
		// Super is implicitly called with an empty ctor, resulting in an empty map of categories.
		this.categoryBound = categoryBound;
	}

	@Override
	public void reset() {
		categories.clear();
		otherCount = 0L;
	}

	@Override
	public void accumulateObject(Object value) {
		MutableLong count = categories.get(value);
		if (count != null) {
			count.increment();
		} else if (categories.size() == categoryBound) {
			otherCount++;
		} else {
			categories.put(value, new MutableLong(1L));
		}
	}
}